package com.tsp_tux.demo.Service;

import com.tsp_tux.demo.Dao.ResourceRepository;
import com.tsp_tux.demo.Entity.RespBean;
import com.tsp_tux.demo.Entity.Resource;
import com.tsp_tux.demo.Util.easyRespBean;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 沈育洋
 */
@Service
public class ResourceServiceImp implements ResourceService {

    final
    ResourceRepository resourceRepository;

    public ResourceServiceImp(ResourceRepository resourceRepository) {
        this.resourceRepository = resourceRepository;
    }

    @Override
    public RespBean searchResource(String search) {
        List<Resource> resources = resourceRepository.findByNameLike(search);
        if (resources != null) {
            return easyRespBean.ok(resources);
        }
        return easyRespBean.noFound();
    }

    @Override
    public RespBean getResourceByRid(int rid) {
        Resource resource = resourceRepository.findByRid(rid);
        if (resource != null) {
            return easyRespBean.ok(resource);
        }
        return easyRespBean.noFound();
    }

    @Override
    public RespBean getResourceByAid(int aid) {
        List<Resource> resources = resourceRepository.findByAid(aid);
        if (resources != null) {
            return easyRespBean.ok(resources);
        }
        return easyRespBean.noFound();
    }

    @Override
    public RespBean getResourceBySid(int sid) {
        List<Resource> resources = resourceRepository.findBySid(sid);
        if (resources != null) {
            return easyRespBean.ok(resources);
        }
        return easyRespBean.noFound();
    }

    @Override
    public RespBean createResource(Resource resource) {
        resourceRepository.save(resource);
        return easyRespBean.ok();
    }
}
